package br.com.massai.model.entity;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Embeddable;
import javax.persistence.Transient;

@Embeddable
public class GenericEntity implements Serializable {

	private static final long serialVersionUID = 3433955329081207690L;

	@Transient
	private StringBuilder clauseWhere;

	@Transient
	private Map<String, Object> parameters;

	protected enum ConditionType {
		AND , OR;
	}

	@Override
	public String toString() {
		StringBuilder str = new StringBuilder("[").append(this.getClass().getSimpleName()).append("] ");
		try{
			Field[] fields = this.getClass().getDeclaredFields();
			for(Field f : fields){
				if(!Modifier.isStatic(f.getModifiers())){
					f.setAccessible(true);
					str.append(f.getName()).append(": ").append(f.get(this)).append(", ");
				}
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();

		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return str.toString();
	}

	protected void addClauseWhere(String clause, ConditionType conditionType) {
		if(this.clauseWhere == null)
			this.clauseWhere = new StringBuilder("");
		this.clauseWhere.append(" ").append(conditionType).append(" ").append(clause);
	}

	public StringBuilder getClauseWhere(){
		return this.clauseWhere;
	}

	protected void addParameters(String key, Object value) {
		if(this.parameters == null)
			this.parameters = new HashMap<String, Object>();
		this.parameters.put(key, value);
	}

	public Map<String, Object> getParameters(){
		return this.parameters;
	}
}